﻿using System;
using System.Collections;
using Component.Track;
using Component.Trains;
using Config;
using UnityEditor;
using UnityEngine;

namespace Component.UI.View
{
    public class PauseView : MonoBehaviour
    {
        
        [Header("暂停界面")]
        [SerializeField] private PauseViewBtn pauseViewBtn;
        
        private float currentCameraSize;
        
        
        private void Start()
        {
            //重新选择按钮
            pauseViewBtn.resetBtn.OnClickStart = GameManager.Instance.ResetGame;
            //退出按钮
            pauseViewBtn.quitBtn.OnClickStart = GameManager.Instance.ResetGame;
            //游戏界面的返回按钮
            pauseViewBtn.backBtn.OnClickStart = delegate
            { 
                currentCameraSize = CameraTools.MainCamera.orthographicSize;
                GameManager.Instance.GamePause();
            };
            //继续按钮
            pauseViewBtn.continueBtn.OnClickStart = delegate
            {
                CameraTools.MainCamera.orthographicSize = currentCameraSize;
                pauseViewBtn.continueBtn.Follow = GameManager.Instance.CurrentTargetPos;
            };
            pauseViewBtn.continueBtn.OnClickFinished = delegate
            {
                ViewManager.GetIntoStartView(ViewManager.ViewType.GameView);

                StartCoroutine(OnClickFinished(.8f, delegate
                {
                    GameManager.Instance.GameUnPause();
                }));
            };
            
            GameManager.Instance.onStartGame += delegate()
            {
                ViewManager.SetPauseViewPoint(pauseViewBtn.follow.position);
            };
            
        }

        private IEnumerator OnClickFinished(float delay, Action callback)
        {
            yield return new WaitForSeconds(delay);
            callback.Invoke();
        }
        
    }


    [Serializable]
    public struct PauseViewBtn
    {
        public UiButton backBtn;
        public UiButton continueBtn;
        public UiButton resetBtn;
        public UiButton quitBtn;
        public Transform follow;
    }
}